package com.dant.components
{
	import flash.events.MouseEvent;
	import flash.text.TextLineMetrics;
	
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.controls.Text;
	import mx.core.UIComponent;
	import mx.core.UITextField;
	import mx.events.CloseEvent;
	import mx.styles.CSSStyleDeclaration;
	import mx.styles.StyleManager;
	
	[Style(name="backgroundColor",type="uint",format="Color",inherit="no")]
	[Style(name="borderThickness",type="int",format="Number",inherit="no")]
	[Style(name="borderColor",type="uint",format="Color",inherit="no")]
	[Style(name="cornerRadius",type="int",format="Number",inherit="no")]
	[Style(name="labelStyleName",type="String",format="String",inherit="no")]
	[Style(name="titleBarBackgroundColor",type="uint",format="color",inherit="no")]
	[Style(name="titleBarHeight",type="int",format="Number",inherit="no")]
	[Style(name="titleLabelColor",type="uint",format="Color",inherit="no")]
	[Style(name="titleLabelStyleName",type="String",format="String",inherit="no")]
	[Style(name="topTransparentHeight",type="int",format="Number",inherit="no")]
	[Style(name="messageImageSkin",type="Class",inherit="no")]
	[Style(name="closeButtonUpSkin",type="Class",inherit="no")]
	[Style(name="closeButtonDownSkin",type="Class",inherit="no")]
	[Style(name="closeButtonOverSkin",type="Class",inherit="no")]
	[Style(name="closeButtonHeight",type="Number",inherit="no")]
	[Style(name="closeButtonWidth",type="Number",inherit="no")]

	[Event(name="close", type="mx.events.CloseEvent")]

    [Effect(name="closeEffect", event="close")]
	public class OutputWindow extends UIComponent
	{
		
	//--------------------------------------------------------------------------
	//
	//  Constants
	//
	//--------------------------------------------------------------------------
        public static const DEFAULT_BACKGROUND_COLOR : uint = 0xCC3333;
        public static const DEFAULT_BORDER_COLOR : uint = 0xF3DD69;
        public static const DEFAULT_BORDER_THICKNESS : Number = 2;
        public static const DEFAULT_COLOR : uint = 0xFFFFFF;
        public static const DEFAULT_CORNER_RADIUS : Number = 9;
        public static const DEFAULT_TITLE_BAR_BACKGROUND_COLOR : uint = 0xCC3333;
        public static const DEFAULT_TITLE_BAR_HEIGHT : int = 50;
        public static const DEFAULT_TITLE_LABEL_COLOR : uint = 0xFFFFFF;
        public static const DEFAULT_TOP_TRANSPARENT_HEIGHT : int = 20;
        public static const DEFAULT_CLOSE_BUTTON_HEIGHT : int = 20;
        public static const DEFAULT_CLOSE_BUTTON_WIDTH : int = 20;
		
		
	//--------------------------------------------------------------------------
	//
	//  Properties
	//
	//--------------------------------------------------------------------------
	    protected static var styleInitialized:Boolean = setDefaultStyle();

        /**
        * 
        * @private
        */
        protected var errorMessageText:Text;
        
        /**
        * 
        *@private 
        */        
        protected var titleBar:UIComponent;

	    /**
	    * 
	    *@private 
	    */
	    protected var titleText:UITextField;
	    
	    /**
	     *
	     * @private 
	     */	    
	    protected var closeButton:Button;
	    
	    /**
	     *
	     * @private 
	     */	    
	    protected var messageImage:Image;
	
		/**
		* @private
		*
		*/
		private var _text : String;
		private var textChanged : Boolean;
		
		
        /**
        * @private
        *
        */
        private var _title : String;
        private var titleChanged : Boolean;

        private var backgroundColorChanged : Boolean = true;
        private var borderThicknessChanged : Boolean = true;
        private var borderColorChanged : Boolean = true;
        private var colorChanged : Boolean = true;
        private var cornerRadiusChanged : Boolean = true;
        private var labelStyleNameChanged : Boolean = true;
        private var titleBarBackgroundColorChanged : Boolean = true;
        private var titleBarHeightChanged : Boolean = true;
        private var titleLabelColorChanged : Boolean = true;
        private var titleLabelStyleNameChanged : Boolean = true;
        private var closeButtonUpSkinChanged:Boolean = true;
        private var closeButtonDownSkinChanged:Boolean = true;
        private var closeButtonOverSkinChanged:Boolean = true;
        private var closeButtonHeightChanged:Boolean = true;
        private var closeButtonWidthChanged:Boolean = true;
        private var messageImageSkinChanged:Boolean = true;

		[Bindable]
		/**
		*
		*
		*/
		public function set text(value:String):void
		{
			if ( _text != value )
			{
				_text = value;
				textChanged = true;
				invalidateProperties();
			}
		}
		
		public function get text():String
		{
			return _text;
		}


		[Bindable]
		/**
		*
		*
		*/
		public function set title(value:String):void
		{
			if ( _title != value )
			{
				_title = value;
				titleChanged = true;
				invalidateProperties();
			}
		}
		
		public function get title():String
		{
			return _title;
		}


        /**
        * @private
        * Defines a method that sets the default values for the style properties.
        */
        private static function setDefaultStyle():Boolean
        {
            if (!StyleManager.getStyleDeclaration("OutputWindow"))
            {
                // If there is no CSS definition for this class, 
                // then create one and set the default value.
                var defaultStyle:CSSStyleDeclaration = new CSSStyleDeclaration();
                
		        var backgroundColor : uint;
		        var borderThickness : Number;
		        var borderColor : uint;
		        var color : uint;
		        var cornerRadius : int;
		        var labelStyleName : String;
		        var titleBarBackgroundColor : uint;
		        var titleBarHeight:Number;
		        var titleLabelColor : uint;
		        var titleLabelStyleName : String;
		        var topTransparentHeight:int;
                
                defaultStyle.defaultFactory = function():void
                {
                    backgroundColor = DEFAULT_BACKGROUND_COLOR;
                    borderThickness = DEFAULT_BORDER_THICKNESS;
                    borderColor = DEFAULT_BORDER_COLOR;
                    color = DEFAULT_COLOR;
                    cornerRadius = DEFAULT_CORNER_RADIUS;
                    titleBarBackgroundColor = DEFAULT_TITLE_BAR_BACKGROUND_COLOR;
                    titleBarHeight = DEFAULT_TITLE_BAR_HEIGHT;
                    titleLabelColor = DEFAULT_TITLE_LABEL_COLOR;
                    topTransparentHeight = DEFAULT_TOP_TRANSPARENT_HEIGHT;
                }
                // Updates the declaration into the CSS
                StyleManager.setStyleDeclaration("OutputWindow", defaultStyle, true);
            }
            return(true);
        }

		public function OutputWindow()
		{
			super();
		}

        override protected function createChildren():void
        {
        	super.createChildren();

        	titleBar = new UIComponent();
        	errorMessageText = new Text();
        	titleText = new UITextField();
        	
        	titleBar.width = width;
        	titleBar.height = 100;
        	
        	titleBar.addChild( titleText );
        	
        	closeButton = new Button();
        	closeButton.addEventListener(MouseEvent.CLICK,closeButton_clickHandler,false,0,true);
//        	closeImage.buttonMode = true;

        	messageImage = new Image();
        	
        	addChild( titleBar );
        	addChild( errorMessageText );
        	addChild( closeButton );
        	addChild( messageImage );
        }

        override protected function childrenCreated():void
        {
        	super.childrenCreated();
        }

        override protected function commitProperties():void
        {
        	super.commitProperties();
        	if ( textChanged )
        	{
        		errorMessageText.text = text;
        		textChanged = false;
        	}
        	
        	if ( titleChanged )
        	{
        		titleText.text = title;
        		titleChanged = false;
        	}
        }
        
        override protected function measure():void
        {
        	super.measure();
        }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
        	super.updateDisplayList(unscaledWidth, unscaledHeight);
        	
        	if ( borderThicknessChanged )
        	{
        		setStyle("borderThickness",getBorderThickness());
        		borderThicknessChanged = false;
        	}
        	
        	if ( borderColorChanged )
        	{
        		setStyle("borderColor",getBorderColor());
        		borderColorChanged = false;
        	}
        	
        	if ( messageImageSkinChanged )
        	{
        		messageImage.source = getMessageImageSkin();
        		messageImageSkinChanged = false;
        	}
        	
        	if ( closeButtonUpSkinChanged )
        	{
        		closeButton.setStyle("upSkin",getCloseButtonUpSkin());
        		closeButtonUpSkinChanged = false;
        	}
        	
            if ( closeButtonDownSkinChanged )
            {
                closeButton.setStyle("downSkin",getCloseButtonDownSkin());
                closeButtonDownSkinChanged = false;
            }

            if ( closeButtonOverSkinChanged )
            {
                closeButton.setStyle("overSkin",getCloseButtonOverSkin());
                closeButtonOverSkinChanged = false;
            }
        	
        	if ( colorChanged )
        	{
        		errorMessageText.setStyle("color",getColor());
        		colorChanged = false;
        	}
        	
        	if ( labelStyleNameChanged )
        	{
        		errorMessageText.styleName = getLabelStyleName();
        		labelStyleNameChanged = false;
        	}
        	
        	if ( titleLabelColorChanged )
        	{
        		titleText.setStyle("color",getTitleLabelColor());
        		titleLabelColorChanged = false;
        	}
        	
        	if ( titleLabelStyleNameChanged )
        	{
        		titleText.styleName = getTitleLabelStyleName();
        		titleLabelStyleNameChanged = false;
        	}

            // closeButton metrics, and positioning
            var closeButtonWidth:Number = getCloseButtonWidth();
            var closeButtonHeight:Number = getCloseButtonHeight();
            closeButton.setActualSize(closeButtonWidth,closeButtonHeight);
            closeButton.move(unscaledWidth - closeButtonWidth - 5, 5 );

            // titleBar metrics and positioning
        	titleBar.move(0,getTopTransparentHeight());
        	titleBar.setActualSize(unscaledWidth,getTitleBarHeight());
        	titleText.setActualSize(titleBar.width,titleBar.height)

        	// message text positioning
        	var middleX:int = unscaledWidth/2;
        	var middleY:int = (unscaledHeight-getTopTransparentHeight())/2;

        	var metrics:TextLineMetrics = errorMessageText.measureText(text);

        	var textXPosition:int = middleX - metrics.width/2;
        	var textYPosition:int = getTopTransparentHeight() + 
        	                                       (middleY - metrics.height/2);

        	errorMessageText.move((textXPosition > 0) ? textXPosition:0,
        	                       (textYPosition > 0) ? textYPosition:0);

            // message text metrics
        	errorMessageText.setActualSize(unscaledWidth,(unscaledHeight-errorMessageText.y));

            // corner radius, transparent space, and background color settings
            var cornerRadius:int = getCornerRadius();
            var topTransparentHeight:int = getBackgroundColor();

            graphics.clear();
            graphics.beginFill(topTransparentHeight,1);
            graphics.drawRoundRectComplex(0,getTopTransparentHeight(),unscaledWidth,unscaledHeight,
                          cornerRadius,cornerRadius,cornerRadius,cornerRadius);

            backgroundColorChanged = false;

            // titleBar backgroundColor and stuff
            titleBar.graphics.clear();
            titleBar.graphics.beginFill(getTitleBarBackgroundColor(),1);
            titleBar.graphics.drawRoundRectComplex(0,0,titleBar.width,titleBar.height,
              cornerRadius,cornerRadius,0,0);
            titleBarBackgroundColorChanged = false;
        }

        override public function styleChanged(styleProp:String):void
        {
        	super.styleChanged(styleProp);
        	if ( styleProp == "backgroundColor" )
        	{
        		backgroundColorChanged = true;
        	}
        	
            if ( styleProp == "borderThickness" )
            {
                borderThicknessChanged = true;
            }        	
        	
            if ( styleProp == "borderColor" )
            {
                borderColorChanged = true;
            }

            if ( styleProp == "color" )
            {
                colorChanged = true;
            }

            if ( styleProp == "cornerRadius" )
            {
                cornerRadiusChanged = true;
            }

            if ( styleProp == "labelStyleName" )
            {
                labelStyleNameChanged = true;
            }

            if ( styleProp == "titleBarColor" )
            {
                titleBarBackgroundColorChanged = true;
            }
            
            if ( styleProp == "titleBarHeight" )
            {
                titleBarHeightChanged = true;
            }

            if ( styleProp == "titleLabelColor" )
            {
                titleLabelColorChanged = true;
            }
            
            if ( styleProp == "titleLabelStyleName" )
            {
                titleLabelStyleNameChanged = true;
            }
            
            if ( styleProp == "messageImageSkin" )
            {
            	messageImageSkinChanged = true;
            }
            
            if ( styleProp == "closeButtonUpSkin" )
            {
            	closeButtonUpSkinChanged = true;
            }
            
            if ( styleProp == "closeButtonDownSkin" )
            {
                closeButtonDownSkinChanged = true;
            }
            
            if ( styleProp == "closeButtonOverSkin" )
            {
                closeButtonOverSkinChanged = true;
            }

            if ( styleProp == "closeButtonWidthChanged" )
            {
                closeButtonWidthChanged = true;
            }

            if ( styleProp == "closeButtonHeightChanged" )
            {
                closeButtonHeightChanged = true;
            }
            
            

            invalidateDisplayList();
        }
        
        //--------------------------------------
        //  Style properties
        //--------------------------------------
        /**
        *
        *
        */
        protected function getBackgroundColor():uint
        {
            var _backgroundColor:uint = getStyle("backgroundColor");
            if ( _backgroundColor > 0 )
            {
                return ( _backgroundColor );
            }
            return ( DEFAULT_BACKGROUND_COLOR );
        }
        
        /**
        *
        *
        */
        protected function getBorderThickness():Number
        {
            var number:Number = getStyle("borderThickness");
            if ( number >= 0 )
            {
                return ( number );
            }
            return ( DEFAULT_BORDER_THICKNESS );
        }

        /**
        *
        *
        */
        protected function getCornerRadius():int
        {
            var number:int = getStyle("cornerRadius");
            if ( number >= 0 )
            {
                return ( number );
            }
            return ( DEFAULT_CORNER_RADIUS );
        }

        /**
        *
        *
        */
        protected function getBorderColor():uint
        {
            var _color:uint = getStyle("borderColor");
            if ( _color > 0 )
            {
                return ( _color );
            }
            return ( DEFAULT_BORDER_COLOR );
        }

        /**
        *
        *
        */
        protected function getLabelStyleName():String
        {
            var _labelStyleName:String = getStyle("labelStyleName");
            return ( _labelStyleName );
        }

        /**
        *
        *
        */
        protected function getColor():uint
        {
            var _color:uint = getStyle("color");
            if ( _color > 0 )
            {
                return ( _color );
            }
            return ( DEFAULT_COLOR );
        }
        
        /**
        *
        *
        */
        protected function getCloseButtonHeight():uint
        {
            var number:Number = getStyle("closeButtonHeight");
            if ( number > 0 )
            {
                return ( number );
            }
            return ( DEFAULT_CLOSE_BUTTON_HEIGHT );
        }
        
        /**
        *
        *
        */
        protected function getCloseButtonWidth():uint
        {
            var number:Number = getStyle("closeButtonWidth");
            if ( number > 0 )
            {
                return ( number );
            }
            return ( DEFAULT_CLOSE_BUTTON_WIDTH );
        }
        
        /**
         * 
         * @return 
         * 
         */        
        protected function getCloseButtonUpSkin():Class
        {
        	var skin:Class = getStyle("closeButtonUpSkin");
        	
        	if ( ! skin )
        	{
        		skin = getStyle("closeButtonDownSkin");
        		if ( ! skin )
        		{
        			skin = getStyle("closeButtonOverSkin");
        		}
        	}
        	return ( skin );
        }

        /**
         * 
         * @return 
         * 
         */        
        protected function getCloseButtonDownSkin():Class
        {
            var skin:Class = getStyle("closeButtonDownSkin");

            if ( ! skin )
            {
                skin = getStyle("closeButtonOverSkin");
                if ( ! skin )
                {
                    skin = getStyle("closeButtonUpSkin");
                }
            }
            return ( skin );
        }

        /**
         * 
         * @return 
         * 
         */        
        protected function getCloseButtonOverSkin():Class
        {
            var skin:Class = getStyle("closeButtonOverSkin");

            if ( ! skin )
            {
                skin = getStyle("closeButtonUpSkin");
                if ( ! skin )
                {
                    skin = getStyle("closeButtonDownSkin");
                }
            }
            return ( skin );
        }

        /**
         * 
         * @return 
         * 
         */        
        protected function getMessageImageSkin():Class
        {
            var skin:Class = getStyle("messageImageSkin");
            return ( skin );
        }

        /**
        *
        *
        */
        protected function getTopTransparentHeight():int
        {
            var _height:int = getStyle("topTransparentHeight");
            if ( _height >= 0 )
            {
                return ( _height );
            }
            return ( DEFAULT_TOP_TRANSPARENT_HEIGHT );
        }

        /**
        *
        *
        */
        protected function getTitleBarHeight():int
        {
            var _height:int = getStyle("titleBarHeight");
            if ( _height > 0 )
            {
                return ( _height );
            }
            return ( DEFAULT_TITLE_BAR_HEIGHT );
        }

        /**
        *
        *
        */
        protected function getTitleBarBackgroundColor():uint
        {
            var _color:uint = getStyle("titleBarBackgroundColor");
            if ( _color > 0 )
            {
                return ( _color );
            }
            return ( getBackgroundColor() );
        }


        /**
        *
        *
        */
        protected function getTitleLabelColor():uint
        {
            var _color:uint = getStyle("titleLabelColor");
            if ( _color > 0 )
            {
                return ( _color );
            }
            return ( DEFAULT_TITLE_LABEL_COLOR );
        }


        /**
        *
        *
        */
        protected function getTitleLabelStyleName():String
        {
            var labelStyleName:String = getStyle("titleLabelStyleName");
            return ( labelStyleName );
        }
        
        
        protected function closeButton_clickHandler (event:MouseEvent):void
        {
        	var closeEvent:CloseEvent = new CloseEvent(CloseEvent.CLOSE);
        	dispatchEvent(closeEvent);
        }

	}
}








